{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<table style=\"float:left; border:none\">\n",
    "   <tr style=\"border:none; background-color: #ffffff\">\n",
    "       <td style=\"border:none\">\n",
    "           <a href=\"http://bokeh.org/\">     \n",
    "           <img \n",
    "               src=\"assets/bokeh-transparent.png\" \n",
    "               style=\"width:50px\"\n",
    "           >\n",
    "           </a>    \n",
    "       </td>\n",
    "       <td style=\"border:none\">\n",
    "           <h1>Bokeh Tutorial</h1>\n",
    "       </td>\n",
    "   </tr>\n",
    "</table>\n",
    "\n",
    "<div style=\"float:right;\"><h2>08. Graph and Network Plots</h2></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This chapter will cover how to plot network node/link graphs in Bokeh using NetwortkX. For information on creating graph renderers from a low level, see [Visualizing Network Graphs](https://bokeh.pydata.org/en/latest/docs/user_guide/graph.html)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "    <div class=\"bk-root\">\n",
       "        <a href=\"https://bokeh.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n",
       "        <span id=\"1001\">Loading BokehJS ...</span>\n",
       "    </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "\n",
       "(function(root) {\n",
       "  function now() {\n",
       "    return new Date();\n",
       "  }\n",
       "\n",
       "  var force = true;\n",
       "\n",
       "  if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n",
       "    root._bokeh_onload_callbacks = [];\n",
       "    root._bokeh_is_loading = undefined;\n",
       "  }\n",
       "\n",
       "  var JS_MIME_TYPE = 'application/javascript';\n",
       "  var HTML_MIME_TYPE = 'text/html';\n",
       "  var EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n",
       "  var CLASS_NAME = 'output_bokeh rendered_html';\n",
       "\n",
       "  /**\n",
       "   * Render data to the DOM node\n",
       "   */\n",
       "  function render(props, node) {\n",
       "    var script = document.createElement(\"script\");\n",
       "    node.appendChild(script);\n",
       "  }\n",
       "\n",
       "  /**\n",
       "   * Handle when an output is cleared or removed\n",
       "   */\n",
       "  function handleClearOutput(event, handle) {\n",
       "    var cell = handle.cell;\n",
       "\n",
       "    var id = cell.output_area._bokeh_element_id;\n",
       "    var server_id = cell.output_area._bokeh_server_id;\n",
       "    // Clean up Bokeh references\n",
       "    if (id != null && id in Bokeh.index) {\n",
       "      Bokeh.index[id].model.document.clear();\n",
       "      delete Bokeh.index[id];\n",
       "    }\n",
       "\n",
       "    if (server_id !== undefined) {\n",
       "      // Clean up Bokeh references\n",
       "      var cmd = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n",
       "      cell.notebook.kernel.execute(cmd, {\n",
       "        iopub: {\n",
       "          output: function(msg) {\n",
       "            var id = msg.content.text.trim();\n",
       "            if (id in Bokeh.index) {\n",
       "              Bokeh.index[id].model.document.clear();\n",
       "              delete Bokeh.index[id];\n",
       "            }\n",
       "          }\n",
       "        }\n",
       "      });\n",
       "      // Destroy server and session\n",
       "      var cmd = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n",
       "      cell.notebook.kernel.execute(cmd);\n",
       "    }\n",
       "  }\n",
       "\n",
       "  /**\n",
       "   * Handle when a new output is added\n",
       "   */\n",
       "  function handleAddOutput(event, handle) {\n",
       "    var output_area = handle.output_area;\n",
       "    var output = handle.output;\n",
       "\n",
       "    // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n",
       "    if ((output.output_type != \"display_data\") || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n",
       "      return\n",
       "    }\n",
       "\n",
       "    var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n",
       "\n",
       "    if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n",
       "      toinsert[toinsert.length - 1].firstChild.textContent = output.data[JS_MIME_TYPE];\n",
       "      // store reference to embed id on output_area\n",
       "      output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n",
       "    }\n",
       "    if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n",
       "      var bk_div = document.createElement(\"div\");\n",
       "      bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n",
       "      var script_attrs = bk_div.children[0].attributes;\n",
       "      for (var i = 0; i < script_attrs.length; i++) {\n",
       "        toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n",
       "      }\n",
       "      // store reference to server id on output_area\n",
       "      output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n",
       "    }\n",
       "  }\n",
       "\n",
       "  function register_renderer(events, OutputArea) {\n",
       "\n",
       "    function append_mime(data, metadata, element) {\n",
       "      // create a DOM node to render to\n",
       "      var toinsert = this.create_output_subarea(\n",
       "        metadata,\n",
       "        CLASS_NAME,\n",
       "        EXEC_MIME_TYPE\n",
       "      );\n",
       "      this.keyboard_manager.register_events(toinsert);\n",
       "      // Render to node\n",
       "      var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n",
       "      render(props, toinsert[toinsert.length - 1]);\n",
       "      element.append(toinsert);\n",
       "      return toinsert\n",
       "    }\n",
       "\n",
       "    /* Handle when an output is cleared or removed */\n",
       "    events.on('clear_output.CodeCell', handleClearOutput);\n",
       "    events.on('delete.Cell', handleClearOutput);\n",
       "\n",
       "    /* Handle when a new output is added */\n",
       "    events.on('output_added.OutputArea', handleAddOutput);\n",
       "\n",
       "    /**\n",
       "     * Register the mime type and append_mime function with output_area\n",
       "     */\n",
       "    OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n",
       "      /* Is output safe? */\n",
       "      safe: true,\n",
       "      /* Index of renderer in `output_area.display_order` */\n",
       "      index: 0\n",
       "    });\n",
       "  }\n",
       "\n",
       "  // register the mime type if in Jupyter Notebook environment and previously unregistered\n",
       "  if (root.Jupyter !== undefined) {\n",
       "    var events = require('base/js/events');\n",
       "    var OutputArea = require('notebook/js/outputarea').OutputArea;\n",
       "\n",
       "    if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n",
       "      register_renderer(events, OutputArea);\n",
       "    }\n",
       "  }\n",
       "\n",
       "  \n",
       "  if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n",
       "    root._bokeh_timeout = Date.now() + 5000;\n",
       "    root._bokeh_failed_load = false;\n",
       "  }\n",
       "\n",
       "  var NB_LOAD_WARNING = {'data': {'text/html':\n",
       "     \"<div style='background-color: #fdd'>\\n\"+\n",
       "     \"<p>\\n\"+\n",
       "     \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
       "     \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
       "     \"</p>\\n\"+\n",
       "     \"<ul>\\n\"+\n",
       "     \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
       "     \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
       "     \"</ul>\\n\"+\n",
       "     \"<code>\\n\"+\n",
       "     \"from bokeh.resources import INLINE\\n\"+\n",
       "     \"output_notebook(resources=INLINE)\\n\"+\n",
       "     \"</code>\\n\"+\n",
       "     \"</div>\"}};\n",
       "\n",
       "  function display_loaded() {\n",
       "    var el = document.getElementById(\"1001\");\n",
       "    if (el != null) {\n",
       "      el.textContent = \"BokehJS is loading...\";\n",
       "    }\n",
       "    if (root.Bokeh !== undefined) {\n",
       "      if (el != null) {\n",
       "        el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n",
       "      }\n",
       "    } else if (Date.now() < root._bokeh_timeout) {\n",
       "      setTimeout(display_loaded, 100)\n",
       "    }\n",
       "  }\n",
       "\n",
       "\n",
       "  function run_callbacks() {\n",
       "    try {\n",
       "      root._bokeh_onload_callbacks.forEach(function(callback) {\n",
       "        if (callback != null)\n",
       "          callback();\n",
       "      });\n",
       "    } finally {\n",
       "      delete root._bokeh_onload_callbacks\n",
       "    }\n",
       "    console.debug(\"Bokeh: all callbacks have finished\");\n",
       "  }\n",
       "\n",
       "  function load_libs(css_urls, js_urls, callback) {\n",
       "    if (css_urls == null) css_urls = [];\n",
       "    if (js_urls == null) js_urls = [];\n",
       "\n",
       "    root._bokeh_onload_callbacks.push(callback);\n",
       "    if (root._bokeh_is_loading > 0) {\n",
       "      console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
       "      return null;\n",
       "    }\n",
       "    if (js_urls == null || js_urls.length === 0) {\n",
       "      run_callbacks();\n",
       "      return null;\n",
       "    }\n",
       "    console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
       "    root._bokeh_is_loading = css_urls.length + js_urls.length;\n",
       "\n",
       "    function on_load() {\n",
       "      root._bokeh_is_loading--;\n",
       "      if (root._bokeh_is_loading === 0) {\n",
       "        console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n",
       "        run_callbacks()\n",
       "      }\n",
       "    }\n",
       "\n",
       "    function on_error() {\n",
       "      console.error(\"failed to load \" + url);\n",
       "    }\n",
       "\n",
       "    for (var i = 0; i < css_urls.length; i++) {\n",
       "      var url = css_urls[i];\n",
       "      const element = document.createElement(\"link\");\n",
       "      element.onload = on_load;\n",
       "      element.onerror = on_error;\n",
       "      element.rel = \"stylesheet\";\n",
       "      element.type = \"text/css\";\n",
       "      element.href = url;\n",
       "      console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n",
       "      document.body.appendChild(element);\n",
       "    }\n",
       "\n",
       "    for (var i = 0; i < js_urls.length; i++) {\n",
       "      var url = js_urls[i];\n",
       "      var element = document.createElement('script');\n",
       "      element.onload = on_load;\n",
       "      element.onerror = on_error;\n",
       "      element.async = false;\n",
       "      element.src = url;\n",
       "      console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
       "      document.head.appendChild(element);\n",
       "    }\n",
       "  };var element = document.getElementById(\"1001\");\n",
       "  if (element == null) {\n",
       "    console.error(\"Bokeh: ERROR: autoload.js configured with elementid '1001' but no matching script tag was found. \")\n",
       "    return false;\n",
       "  }\n",
       "\n",
       "  function inject_raw_css(css) {\n",
       "    const element = document.createElement(\"style\");\n",
       "    element.appendChild(document.createTextNode(css));\n",
       "    document.body.appendChild(element);\n",
       "  }\n",
       "\n",
       "  \n",
       "  var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-1.4.0.min.js\"];\n",
       "  var css_urls = [];\n",
       "  \n",
       "\n",
       "  var inline_js = [\n",
       "    function(Bokeh) {\n",
       "      Bokeh.set_log_level(\"info\");\n",
       "    },\n",
       "    function(Bokeh) {\n",
       "    \n",
       "    \n",
       "    }\n",
       "  ];\n",
       "\n",
       "  function run_inline_js() {\n",
       "    \n",
       "    if (root.Bokeh !== undefined || force === true) {\n",
       "      \n",
       "    for (var i = 0; i < inline_js.length; i++) {\n",
       "      inline_js[i].call(root, root.Bokeh);\n",
       "    }\n",
       "    if (force === true) {\n",
       "        display_loaded();\n",
       "      }} else if (Date.now() < root._bokeh_timeout) {\n",
       "      setTimeout(run_inline_js, 100);\n",
       "    } else if (!root._bokeh_failed_load) {\n",
       "      console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
       "      root._bokeh_failed_load = true;\n",
       "    } else if (force !== true) {\n",
       "      var cell = $(document.getElementById(\"1001\")).parents('.cell').data().cell;\n",
       "      cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
       "    }\n",
       "\n",
       "  }\n",
       "\n",
       "  if (root._bokeh_is_loading === 0) {\n",
       "    console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
       "    run_inline_js();\n",
       "  } else {\n",
       "    load_libs(css_urls, js_urls, function() {\n",
       "      console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n",
       "      run_inline_js();\n",
       "    });\n",
       "  }\n",
       "}(window));"
      ],
      "application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n  function now() {\n    return new Date();\n  }\n\n  var force = true;\n\n  if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n    root._bokeh_onload_callbacks = [];\n    root._bokeh_is_loading = undefined;\n  }\n\n  \n\n  \n  if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n    root._bokeh_timeout = Date.now() + 5000;\n    root._bokeh_failed_load = false;\n  }\n\n  var NB_LOAD_WARNING = {'data': {'text/html':\n     \"<div style='background-color: #fdd'>\\n\"+\n     \"<p>\\n\"+\n     \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n     \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n     \"</p>\\n\"+\n     \"<ul>\\n\"+\n     \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n     \"<li>use INLINE resources instead, as so:</li>\\n\"+\n     \"</ul>\\n\"+\n     \"<code>\\n\"+\n     \"from bokeh.resources import INLINE\\n\"+\n     \"output_notebook(resources=INLINE)\\n\"+\n     \"</code>\\n\"+\n     \"</div>\"}};\n\n  function display_loaded() {\n    var el = document.getElementById(\"1001\");\n    if (el != null) {\n      el.textContent = \"BokehJS is loading...\";\n    }\n    if (root.Bokeh !== undefined) {\n      if (el != null) {\n        el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n      }\n    } else if (Date.now() < root._bokeh_timeout) {\n      setTimeout(display_loaded, 100)\n    }\n  }\n\n\n  function run_callbacks() {\n    try {\n      root._bokeh_onload_callbacks.forEach(function(callback) {\n        if (callback != null)\n          callback();\n      });\n    } finally {\n      delete root._bokeh_onload_callbacks\n    }\n    console.debug(\"Bokeh: all callbacks have finished\");\n  }\n\n  function load_libs(css_urls, js_urls, callback) {\n    if (css_urls == null) css_urls = [];\n    if (js_urls == null) js_urls = [];\n\n    root._bokeh_onload_callbacks.push(callback);\n    if (root._bokeh_is_loading > 0) {\n      console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n      return null;\n    }\n    if (js_urls == null || js_urls.length === 0) {\n      run_callbacks();\n      return null;\n    }\n    console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n    root._bokeh_is_loading = css_urls.length + js_urls.length;\n\n    function on_load() {\n      root._bokeh_is_loading--;\n      if (root._bokeh_is_loading === 0) {\n        console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n        run_callbacks()\n      }\n    }\n\n    function on_error() {\n      console.error(\"failed to load \" + url);\n    }\n\n    for (var i = 0; i < css_urls.length; i++) {\n      var url = css_urls[i];\n      const element = document.createElement(\"link\");\n      element.onload = on_load;\n      element.onerror = on_error;\n      element.rel = \"stylesheet\";\n      element.type = \"text/css\";\n      element.href = url;\n      console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n      document.body.appendChild(element);\n    }\n\n    for (var i = 0; i < js_urls.length; i++) {\n      var url = js_urls[i];\n      var element = document.createElement('script');\n      element.onload = on_load;\n      element.onerror = on_error;\n      element.async = false;\n      element.src = url;\n      console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n      document.head.appendChild(element);\n    }\n  };var element = document.getElementById(\"1001\");\n  if (element == null) {\n    console.error(\"Bokeh: ERROR: autoload.js configured with elementid '1001' but no matching script tag was found. \")\n    return false;\n  }\n\n  function inject_raw_css(css) {\n    const element = document.createElement(\"style\");\n    element.appendChild(document.createTextNode(css));\n    document.body.appendChild(element);\n  }\n\n  \n  var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-1.4.0.min.js\"];\n  var css_urls = [];\n  \n\n  var inline_js = [\n    function(Bokeh) {\n      Bokeh.set_log_level(\"info\");\n    },\n    function(Bokeh) {\n    \n    \n    }\n  ];\n\n  function run_inline_js() {\n    \n    if (root.Bokeh !== undefined || force === true) {\n      \n    for (var i = 0; i < inline_js.length; i++) {\n      inline_js[i].call(root, root.Bokeh);\n    }\n    if (force === true) {\n        display_loaded();\n      }} else if (Date.now() < root._bokeh_timeout) {\n      setTimeout(run_inline_js, 100);\n    } else if (!root._bokeh_failed_load) {\n      console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n      root._bokeh_failed_load = true;\n    } else if (force !== true) {\n      var cell = $(document.getElementById(\"1001\")).parents('.cell').data().cell;\n      cell.output_area.append_execute_result(NB_LOAD_WARNING)\n    }\n\n  }\n\n  if (root._bokeh_is_loading === 0) {\n    console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n    run_inline_js();\n  } else {\n    load_libs(css_urls, js_urls, function() {\n      console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n      run_inline_js();\n    });\n  }\n}(window));"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.io import show, output_notebook\n",
    "from bokeh.plotting import figure\n",
    "\n",
    "output_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotting from NetworkX\n",
    "\n",
    "The easiest way to plot network graphs with Bokeh is to use the `from_networkx` function. This function accepts any NetworkX graph and returns a Bokeh `GraphRenderer` that can be added to a plot. The `GraphRenderer` has `node_renderer` and `edge_renderer` properties that contain the Bokeh renderers that draw the nodes and edges, respectively. \n",
    "\n",
    "The example below shows a Bokeh plot of `nx.desargues_graph()`, setting some of the node and edge properties."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"9987cf61-e0a4-448e-8dd1-f08096aaf3ff\" data-root-id=\"1004\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  var docs_json = {\"eeeadeb2-9f5d-43dc-9bb3-907542ba44ec\":{\"roots\":{\"references\":[{\"attributes\":{\"renderers\":[{\"id\":\"1009\",\"type\":\"GraphRenderer\"}],\"title\":{\"id\":\"1028\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"1027\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"1002\",\"type\":\"Range1d\"},\"x_scale\":{\"id\":\"1029\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"1003\",\"type\":\"Range1d\"},\"y_scale\":{\"id\":\"1030\",\"type\":\"LinearScale\"}},\"id\":\"1004\",\"type\":\"Plot\"},{\"attributes\":{\"callback\":null,\"end\":2,\"start\":-2},\"id\":\"1002\",\"type\":\"Range1d\"},{\"attributes\":{\"callback\":null,\"data\":{\"end\":[1,19,5,2,16,3,11,4,14,5,9,6,7,15,8,18,9,13,10,11,19,12,13,17,14,15,16,17,18,19],\"start\":[0,0,0,1,1,2,2,3,3,4,4,5,6,6,7,7,8,8,9,10,10,11,12,12,13,14,15,16,17,18]},\"selected\":{\"id\":\"1041\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1042\",\"type\":\"UnionRenderers\"}},\"id\":\"1015\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"data_source\":{\"id\":\"1015\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1014\",\"type\":\"MultiLine\"},\"hover_glyph\":null,\"muted_glyph\":null,\"view\":{\"id\":\"1017\",\"type\":\"CDSView\"}},\"id\":\"1016\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"graph_layout\":{\"0\":[-0.006950453444723943,1.4484501000420282],\"1\":[0.6711723483013132,0.39448876723634624],\"10\":[-0.37554151689986276,1.0515186740080424],\"11\":[0.17655421464522353,-0.07561767715312893],\"12\":[-0.6932710451736761,-1.034289172200008],\"13\":[-0.052318729554820004,-1.2666463534067725],\"14\":[1.0936927975718669,-1.4278579606864945],\"15\":[0.38403657824442355,-1.2673060029390468],\"16\":[-0.05845483800684383,-0.6289657247597874],\"17\":[-1.3194734340038936,-0.5611479383047034],\"18\":[-1.785632122513054,0.5310121379569471],\"19\":[-1.100783001275823,1.43240084487212],\"2\":[1.2854793286356887,-0.12405802976301328],\"3\":[1.8,-0.5289534761187006],\"4\":[1.3852753880742263,0.5956228696807021],\"5\":[0.34838114611929155,0.6672582545650682],\"6\":[-0.37914108021917553,-0.33687983372178915],\"7\":[-1.3439339495625138,0.051754071888239966],\"8\":[-0.4573257889872059,0.0563708903860746],\"9\":[0.42823415804955367,1.0228455584178757]}},\"id\":\"1018\",\"type\":\"StaticLayoutProvider\"},{\"attributes\":{},\"id\":\"1043\",\"type\":\"Selection\"},{\"attributes\":{\"source\":{\"id\":\"1015\",\"type\":\"ColumnDataSource\"}},\"id\":\"1017\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"1044\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1040\",\"type\":\"NodesOnly\"},{\"attributes\":{},\"id\":\"1039\",\"type\":\"NodesOnly\"},{\"attributes\":{\"text\":\"\"},\"id\":\"1028\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"1029\",\"type\":\"LinearScale\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\"},\"id\":\"1027\",\"type\":\"Toolbar\"},{\"attributes\":{\"edge_renderer\":{\"id\":\"1016\",\"type\":\"GlyphRenderer\"},\"inspection_policy\":{\"id\":\"1040\",\"type\":\"NodesOnly\"},\"layout_provider\":{\"id\":\"1018\",\"type\":\"StaticLayoutProvider\"},\"node_renderer\":{\"id\":\"1012\",\"type\":\"GlyphRenderer\"},\"selection_policy\":{\"id\":\"1039\",\"type\":\"NodesOnly\"}},\"id\":\"1009\",\"type\":\"GraphRenderer\"},{\"attributes\":{\"callback\":null,\"data\":{\"index\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]},\"selected\":{\"id\":\"1043\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1044\",\"type\":\"UnionRenderers\"}},\"id\":\"1011\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1041\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1030\",\"type\":\"LinearScale\"},{\"attributes\":{\"fill_color\":{\"value\":\"orange\"},\"size\":{\"units\":\"screen\",\"value\":20}},\"id\":\"1010\",\"type\":\"Circle\"},{\"attributes\":{\"data_source\":{\"id\":\"1011\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1010\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null,\"view\":{\"id\":\"1013\",\"type\":\"CDSView\"}},\"id\":\"1012\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"line_dash\":[2,2]},\"id\":\"1014\",\"type\":\"MultiLine\"},{\"attributes\":{},\"id\":\"1042\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"callback\":null,\"end\":2,\"start\":-2},\"id\":\"1003\",\"type\":\"Range1d\"},{\"attributes\":{\"source\":{\"id\":\"1011\",\"type\":\"ColumnDataSource\"}},\"id\":\"1013\",\"type\":\"CDSView\"}],\"root_ids\":[\"1004\"]},\"title\":\"Bokeh Application\",\"version\":\"1.4.0\"}};\n",
       "  var render_items = [{\"docid\":\"eeeadeb2-9f5d-43dc-9bb3-907542ba44ec\",\"roots\":{\"1004\":\"9987cf61-e0a4-448e-8dd1-f08096aaf3ff\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    var attempts = 0;\n",
       "    var timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "1004"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import networkx as nx\n",
    "from bokeh.models import Range1d, Plot\n",
    "from bokeh.models.graphs import from_networkx\n",
    "\n",
    "G = nx.desargues_graph()\n",
    "\n",
    "# We could use figure here but don't want all the axes and titles\n",
    "plot = Plot(x_range=Range1d(-2, 2), y_range=Range1d(-2, 2))\n",
    "\n",
    "# Create a Bokeh graph from the NetworkX input using nx.spring_layout\n",
    "graph = from_networkx(G, nx.spring_layout, scale=1.8, center=(0,0))\n",
    "plot.renderers.append(graph)\n",
    "\n",
    "# Set some of the default node glyph (Circle) properties\n",
    "graph.node_renderer.glyph.update(size=20, fill_color=\"orange\")\n",
    "\n",
    "# Set some edge properties too\n",
    "graph.edge_renderer.glyph.line_dash = [2,2]\n",
    "\n",
    "show(plot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise: try a different NetworkX layout, and set some properies on `graph.edge_renderer.glyph` \n",
    "# and `graph.node_renderer.glyph`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding Extra Data Columns.\n",
    "\n",
    "The `node_renderer` and `edge_renderer` properties of the graph renderer each have a `data_source` that is a standard `ColumnDataSource` that you can add new data to, e.g. to drive a hover tool, or to specify colors for the renderer. The example below demonstates both."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"15cc7b53-73d1-4fc0-b404-c84440538196\" data-root-id=\"1175\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  var docs_json = {\"ae58f540-f431-4124-88bc-ec37aaa9f417\":{\"roots\":{\"references\":[{\"attributes\":{\"renderers\":[{\"id\":\"1180\",\"type\":\"GraphRenderer\"}],\"title\":{\"id\":\"1223\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"1203\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"1173\",\"type\":\"Range1d\"},\"x_scale\":{\"id\":\"1224\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"1174\",\"type\":\"Range1d\"},\"y_scale\":{\"id\":\"1225\",\"type\":\"LinearScale\"}},\"id\":\"1175\",\"type\":\"Plot\"},{\"attributes\":{\"graph_layout\":{\"0\":[-0.4666918133523442,1.1039409990527946],\"1\":[-0.4261812538340249,0.1892165428668234],\"10\":[1.5755842936515654,0.2333757757804278],\"11\":[1.5925338008830225,-0.7564214638087435],\"12\":[0.9121817553896816,-1.7508035582023447],\"13\":[0.47554305470920116,-1.1052091951529857],\"14\":[-0.3436153960686707,-0.2719343480735946],\"15\":[-1.5798774747187354,-0.2290561685504304],\"16\":[-1.0882550767481591,-0.989960297250036],\"17\":[-0.255192663109464,-1.8000000000000003],\"18\":[-0.3180351258082307,-0.881889196526256],\"19\":[0.3494579910400861,0.27255037890223055],\"2\":[0.8847576145722503,0.11213262444898368],\"3\":[0.32015055897091,0.882883018986142],\"4\":[0.26812912527354865,1.7960204081183202],\"5\":[-0.9044039543776106,1.7581786955104932],\"6\":[-1.5881498548022106,0.7609249027296624],\"7\":[-0.898373958084445,-0.11829617003135076],\"8\":[0.4028155804391965,-0.189882137816395],\"9\":[1.087622795974435,0.9842291890162612]}},\"id\":\"1189\",\"type\":\"StaticLayoutProvider\"},{\"attributes\":{},\"id\":\"1224\",\"type\":\"LinearScale\"},{\"attributes\":{\"callback\":null,\"end\":2,\"start\":-2},\"id\":\"1173\",\"type\":\"Range1d\"},{\"attributes\":{\"callback\":null,\"data\":{\"colors\":[\"#1f77b4\",\"#aec7e8\",\"#ff7f0e\",\"#ffbb78\",\"#2ca02c\",\"#98df8a\",\"#d62728\",\"#ff9896\",\"#9467bd\",\"#c5b0d5\",\"#8c564b\",\"#c49c94\",\"#e377c2\",\"#f7b6d2\",\"#7f7f7f\",\"#c7c7c7\",\"#bcbd22\",\"#dbdb8d\",\"#17becf\",\"#9edae5\"],\"index\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]},\"selected\":{\"id\":\"1238\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1239\",\"type\":\"UnionRenderers\"}},\"id\":\"1182\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1225\",\"type\":\"LinearScale\"},{\"attributes\":{\"edge_renderer\":{\"id\":\"1187\",\"type\":\"GlyphRenderer\"},\"inspection_policy\":{\"id\":\"1235\",\"type\":\"NodesOnly\"},\"layout_provider\":{\"id\":\"1189\",\"type\":\"StaticLayoutProvider\"},\"node_renderer\":{\"id\":\"1183\",\"type\":\"GlyphRenderer\"},\"selection_policy\":{\"id\":\"1234\",\"type\":\"NodesOnly\"}},\"id\":\"1180\",\"type\":\"GraphRenderer\"},{\"attributes\":{},\"id\":\"1236\",\"type\":\"Selection\"},{\"attributes\":{\"fill_color\":{\"field\":\"colors\"},\"size\":{\"units\":\"screen\",\"value\":20}},\"id\":\"1181\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1239\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1234\",\"type\":\"NodesOnly\"},{\"attributes\":{\"source\":{\"id\":\"1182\",\"type\":\"ColumnDataSource\"}},\"id\":\"1184\",\"type\":\"CDSView\"},{\"attributes\":{\"text\":\"\"},\"id\":\"1223\",\"type\":\"Title\"},{\"attributes\":{\"data_source\":{\"id\":\"1182\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1181\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null,\"view\":{\"id\":\"1184\",\"type\":\"CDSView\"}},\"id\":\"1183\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"1238\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1185\",\"type\":\"MultiLine\"},{\"attributes\":{\"source\":{\"id\":\"1186\",\"type\":\"ColumnDataSource\"}},\"id\":\"1188\",\"type\":\"CDSView\"},{\"attributes\":{\"callback\":null,\"tooltips\":\"index: @index\"},\"id\":\"1202\",\"type\":\"HoverTool\"},{\"attributes\":{\"data_source\":{\"id\":\"1186\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1185\",\"type\":\"MultiLine\"},\"hover_glyph\":null,\"muted_glyph\":null,\"view\":{\"id\":\"1188\",\"type\":\"CDSView\"}},\"id\":\"1187\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"callback\":null,\"data\":{\"end\":[1,19,5,2,16,3,11,4,14,5,9,6,7,15,8,18,9,13,10,11,19,12,13,17,14,15,16,17,18,19],\"start\":[0,0,0,1,1,2,2,3,3,4,4,5,6,6,7,7,8,8,9,10,10,11,12,12,13,14,15,16,17,18]},\"selected\":{\"id\":\"1236\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1237\",\"type\":\"UnionRenderers\"}},\"id\":\"1186\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"callback\":null,\"end\":2,\"start\":-2},\"id\":\"1174\",\"type\":\"Range1d\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"1202\",\"type\":\"HoverTool\"}]},\"id\":\"1203\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"1237\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1235\",\"type\":\"NodesOnly\"}],\"root_ids\":[\"1175\"]},\"title\":\"Bokeh Application\",\"version\":\"1.4.0\"}};\n",
       "  var render_items = [{\"docid\":\"ae58f540-f431-4124-88bc-ec37aaa9f417\",\"roots\":{\"1175\":\"15cc7b53-73d1-4fc0-b404-c84440538196\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    var attempts = 0;\n",
       "    var timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "1175"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.models import HoverTool\n",
    "from bokeh.palettes import Category20_20\n",
    "\n",
    "G = nx.desargues_graph() # always 20 nodes\n",
    "\n",
    "# We could use figure here but don't want all the axes and titles\n",
    "plot = Plot(x_range=Range1d(-2, 2), y_range=Range1d(-2, 2))\n",
    "\n",
    "# Create a Bokeh graph from the NetworkX input using nx.spring_layout\n",
    "graph = from_networkx(G, nx.spring_layout, scale=1.8, center=(0,0))\n",
    "plot.renderers.append(graph)\n",
    "\n",
    "# Add some new columns to the node renderer data source\n",
    "graph.node_renderer.data_source.data['index'] = list(range(len(G)))\n",
    "graph.node_renderer.data_source.data['colors'] = Category20_20\n",
    "\n",
    "graph.node_renderer.glyph.update(size=20, fill_color=\"colors\")\n",
    "\n",
    "plot.add_tools(HoverTool(tooltips=\"index: @index\"))\n",
    "\n",
    "show(plot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise: Add your own columns for other node or edge properties e.g. fill_alpha or line_color,\n",
    "# or to show other fields in a tooltoip\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Inspection and Selection Policies\n",
    "\n",
    "Bokeh graph renderers have `inspection_policy` and `selection_policy` properties. These can be used to control how hover inspections highlight the graph, or how selection tools make selections. These properties may be set to any of the inpection policies in `bokeh.graphs`. For instance, if a user hovers over a node, you may wish to highlight all the associated edges as well. This can be accomplished by setting the inspection policy:\n",
    "\n",
    "    graph.inspection_policy = NodesAndLinkedEdges()\n",
    "    \n",
    "as the example below demonstrates."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"1f865a75-f23e-4fab-9dfe-52476d2329ed\" data-root-id=\"1370\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  var docs_json = {\"874b1f2e-5e1f-4b64-aacc-11866f7b2f50\":{\"roots\":{\"references\":[{\"attributes\":{\"renderers\":[{\"id\":\"1375\",\"type\":\"GraphRenderer\"}],\"title\":{\"id\":\"1446\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"1408\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"1368\",\"type\":\"Range1d\"},\"x_scale\":{\"id\":\"1447\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"1369\",\"type\":\"Range1d\"},\"y_scale\":{\"id\":\"1448\",\"type\":\"LinearScale\"}},\"id\":\"1370\",\"type\":\"Plot\"},{\"attributes\":{\"fill_color\":{\"value\":\"#2b83ba\"},\"size\":{\"units\":\"screen\",\"value\":25}},\"id\":\"1385\",\"type\":\"Circle\"},{\"attributes\":{\"data_source\":{\"id\":\"1381\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1390\",\"type\":\"MultiLine\"},\"hover_glyph\":{\"id\":\"1400\",\"type\":\"MultiLine\"},\"muted_glyph\":null,\"view\":{\"id\":\"1383\",\"type\":\"CDSView\"}},\"id\":\"1382\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"1458\",\"type\":\"Selection\"},{\"attributes\":{\"source\":{\"id\":\"1377\",\"type\":\"ColumnDataSource\"}},\"id\":\"1379\",\"type\":\"CDSView\"},{\"attributes\":{\"callback\":null,\"end\":2,\"start\":-2},\"id\":\"1368\",\"type\":\"Range1d\"},{\"attributes\":{\"source\":{\"id\":\"1381\",\"type\":\"ColumnDataSource\"}},\"id\":\"1383\",\"type\":\"CDSView\"},{\"attributes\":{\"line_alpha\":{\"value\":0.8},\"line_color\":{\"value\":\"#cccccc\"},\"line_width\":{\"value\":2}},\"id\":\"1390\",\"type\":\"MultiLine\"},{\"attributes\":{\"fill_color\":{\"value\":\"#abdda4\"},\"size\":{\"units\":\"screen\",\"value\":25}},\"id\":\"1395\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1448\",\"type\":\"LinearScale\"},{\"attributes\":{\"line_color\":{\"value\":\"#abdda4\"},\"line_width\":{\"value\":4}},\"id\":\"1400\",\"type\":\"MultiLine\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"1407\",\"type\":\"HoverTool\"}]},\"id\":\"1408\",\"type\":\"Toolbar\"},{\"attributes\":{\"callback\":null,\"end\":2,\"start\":-2},\"id\":\"1369\",\"type\":\"Range1d\"},{\"attributes\":{\"text\":\"\"},\"id\":\"1446\",\"type\":\"Title\"},{\"attributes\":{\"callback\":null,\"data\":{\"end\":[9,8,4,7,6,3,1,7,12,10,3,11,10,13,4,14,5,7,8,12,9,7,8,14,8,9,13,14,13,14],\"start\":[0,0,0,0,0,0,0,1,1,1,2,2,2,2,3,4,4,4,4,5,5,5,6,6,7,8,10,10,11,13]},\"selected\":{\"id\":\"1458\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1459\",\"type\":\"UnionRenderers\"}},\"id\":\"1381\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1447\",\"type\":\"LinearScale\"},{\"attributes\":{\"callback\":null,\"tooltips\":null},\"id\":\"1407\",\"type\":\"HoverTool\"},{\"attributes\":{},\"id\":\"1460\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1405\",\"type\":\"NodesAndLinkedEdges\"},{\"attributes\":{\"graph_layout\":{\"0\":[0.04467224285005683,0.5922660832769929],\"1\":[-0.6260108426139136,-0.01571818539947648],\"10\":[-0.02318256061521052,-0.8731130568522638],\"11\":[0.9109296809334172,-1.8],\"12\":[-1.3859512430610794,0.38225921683766473],\"13\":[0.4284514089193349,-1.3416015743746341],\"14\":[0.32353747389994403,-0.43173233021633267],\"2\":[0.724872815694703,-1.066362396220879],\"3\":[0.5659577146624951,-0.11662188615960799],\"4\":[-0.08471122299130218,0.3118957184618494],\"5\":[-0.861573516211615,0.871237146323338],\"6\":[0.6255695235919031,0.4661837561785056],\"7\":[-0.4533831993021955,0.6722105117804907],\"8\":[0.14062279866499902,1.0029602013786487],\"9\":[-0.32980107442153894,1.346136794985705]}},\"id\":\"1384\",\"type\":\"StaticLayoutProvider\"},{\"attributes\":{\"callback\":null,\"data\":{\"index\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]},\"selected\":{\"id\":\"1460\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1461\",\"type\":\"UnionRenderers\"}},\"id\":\"1377\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1461\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1459\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"data_source\":{\"id\":\"1377\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1385\",\"type\":\"Circle\"},\"hover_glyph\":{\"id\":\"1395\",\"type\":\"Circle\"},\"muted_glyph\":null,\"view\":{\"id\":\"1379\",\"type\":\"CDSView\"}},\"id\":\"1378\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"edge_renderer\":{\"id\":\"1382\",\"type\":\"GlyphRenderer\"},\"inspection_policy\":{\"id\":\"1405\",\"type\":\"NodesAndLinkedEdges\"},\"layout_provider\":{\"id\":\"1384\",\"type\":\"StaticLayoutProvider\"},\"node_renderer\":{\"id\":\"1378\",\"type\":\"GlyphRenderer\"},\"selection_policy\":{\"id\":\"1457\",\"type\":\"NodesOnly\"}},\"id\":\"1375\",\"type\":\"GraphRenderer\"},{\"attributes\":{},\"id\":\"1457\",\"type\":\"NodesOnly\"}],\"root_ids\":[\"1370\"]},\"title\":\"Bokeh Application\",\"version\":\"1.4.0\"}};\n",
       "  var render_items = [{\"docid\":\"874b1f2e-5e1f-4b64-aacc-11866f7b2f50\",\"roots\":{\"1370\":\"1f865a75-f23e-4fab-9dfe-52476d2329ed\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    var attempts = 0;\n",
       "    var timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "1370"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.models.graphs import NodesAndLinkedEdges\n",
    "from bokeh.models import Circle, HoverTool, MultiLine\n",
    "\n",
    "G = nx.gnm_random_graph(15, 30)\n",
    "\n",
    "# We could use figure here but don't want all the axes and titles\n",
    "plot = Plot(x_range=Range1d(-2, 2), y_range=Range1d(-2 ,2))\n",
    "\n",
    "# Create a Bokeh graph from the NetworkX input using nx.spring_layout\n",
    "graph = from_networkx(G, nx.spring_layout, scale=1.8, center=(0,0))\n",
    "plot.renderers.append(graph)\n",
    "\n",
    "# Blue circles for nodes, and light grey lines for edges\n",
    "graph.node_renderer.glyph = Circle(size=25, fill_color='#2b83ba')\n",
    "graph.edge_renderer.glyph = MultiLine(line_color=\"#cccccc\", line_alpha=0.8, line_width=2)\n",
    "\n",
    "# green hover for both nodes and edges\n",
    "graph.node_renderer.hover_glyph = Circle(size=25, fill_color='#abdda4')\n",
    "graph.edge_renderer.hover_glyph = MultiLine(line_color='#abdda4', line_width=4)\n",
    "\n",
    "# When we hover over nodes, highlight adjecent edges too\n",
    "graph.inspection_policy = NodesAndLinkedEdges()\n",
    "\n",
    "plot.add_tools(HoverTool(tooltips=None))\n",
    "\n",
    "show(plot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise: try a different inspection (or selection) policy like NodesOnly or EdgesAndLinkedNodes\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
